package com.auto.autoapp.ui.activity

import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.GridLayoutManager
import com.auto.autoapp.R
import com.auto.autoapp.adapter.CmdAdapter
import com.auto.autoapp.base.BaseActivity
import com.auto.autoapp.databinding.ActivityCmdSetBinding
import com.auto.autoapp.model.CmdInfo
import com.auto.autoapp.model.cmdList
import com.auto.autoapp.task.ChangeIndexTab
import com.auto.autoapp.task.GetDyCodeTask
import com.auto.autoapp.task.SearchTask
import com.auto.autoapp.task.ToDyIndex
import com.auto.autoapp.util.toast
import com.auto.autoapp.widgets.SimpleToolbar
import com.kongzue.dialogx.dialogs.InputDialog
import com.kongzue.dialogx.interfaces.OnInputDialogButtonClickListener
import com.ven.assists.stepper.StepManager


class  CmdSetActivity : BaseActivity() {

    lateinit var binding: ActivityCmdSetBinding
    val cmdAdapter = CmdAdapter()
    override fun provideView(): View {
        binding = ActivityCmdSetBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun onInit() {
        binding.rvCmd.apply {
            adapter = cmdAdapter
            layoutManager = GridLayoutManager(this@CmdSetActivity, 3)
        }

        cmdAdapter.setList(cmdList)
        cmdAdapter.setOnItemClickListener { adapter, view, position ->
            val item = adapter.getItem(position) as CmdInfo
            if (item.needInput) {
                needInput(item)
            } else {
                StepManager.isStop = true
                StepManager.execute(item.clazz, 1, begin = true)
            }
        }
    }

    override fun configToolbar(toolbar: SimpleToolbar) {
        toolbar.setTitle("指令集")
    }

    fun needInput(item: CmdInfo) {
        InputDialog(item.title ?: item.name, item.hint, "确定", "取消", "")
            .setCancelable(true)
            .setOkButton(object : OnInputDialogButtonClickListener<InputDialog?> {
                override fun onClick(
                    baseDialog: InputDialog?,
                    v: View?,
                    inputStr: String?
                ): Boolean {
                    if (inputStr.isNullOrEmpty()) {
                        toast("输入内容")
                        return true
                    }
                    StepManager.isStop = true
                    StepManager.execute(item.clazz, 1, begin = true, data = inputStr)
                    return false
                }
            })
            .show()
    }
}